//----------------------------------------------------------------------------------------
// @Project Includes
#include "includes.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module 
/* System state control block */
stc_state_t g_stcState;

//----------------------------------------------------------------------------------------
// @Prototypes of functions in the module

//----------------------------------------------------------------------------------------
/**
 * @brief  system state init
 * @param  none
 * @return none
 * @note   none
 */
void app_state_init(void)
{
    /* Init the varible */
    (void)memset((void *)&g_stcState, 0, sizeof(g_stcState));
}

/**
 * @brief  Update system state
 * @param  none
 * @return none
 * @note   none
 */
void app_state_update(void)
{
    static uint16_t s_u16cn10ms = 0;
	
    if (g_stcVar.u32totalSumVolt ==0 )
    {
        return ;
    }		
	
    switch(g_stcState.enstate)
    {
        case SST_INIT:			
            if(g_stcVar.i32avgCurrent >= g_stcParm.stcConfig.u16curThr)
            { 
                if (s_u16cn10ms++ >= 3)
                {
                    g_stcState.enstate = SST_CHG;
										s_u16cn10ms = 0;
                }
            }
            else if(g_stcVar.i32avgCurrent <= -g_stcParm.stcConfig.u16curThr)
            {
                if (s_u16cn10ms++ >= 3)
                {
                    g_stcState.enstate = SST_DSG;
										s_u16cn10ms = 0;
                }            
						}
            else
            {
                if (s_u16cn10ms++ >= 3)
                {
                    g_stcState.enstate = SST_DSG;
										s_u16cn10ms = 0;
                }             
            }
            break;
        case SST_DSG:
            if(g_stcVar.i32avgCurrent >= g_stcParm.stcConfig.u16curThr)
            {	
                if (s_u16cn10ms++ >= 3)
                {
                    g_stcState.enstate = SST_CHG;
                }
            }			
            break;
        case SST_CHG:
            if((g_stcVar.i32avgCurrent <= -g_stcParm.stcConfig.u16curThr)||(g_stcVar.i32avgCurrent == 0))	
            {
                if (s_u16cn10ms++ >= 3)
                {
                    g_stcState.enstate = SST_DSG;
                } 
            }			
            break;
        default:
                if (s_u16cn10ms++ >= 3)
                {
                    g_stcState.enstate = SST_DSG;
										s_u16cn10ms = 0;
                }             
            break;
    }
}

// =======================================================================================
// End of file.
// =======================================================================================
